import { teacherInfo, CompetitionPostData, fileLists } from '@/types/competition'
import { FormInstance, FormRules } from 'element-plus'
import { file } from 'jszip'

export function postData(data?: any) {
  const refForm = ref<FormInstance>()
  /**竞赛基本信息 */
  const formBase = ref<CompetitionPostData>({
    id: '',
    name: '',
    conductUnit: '',
    organizer: '',
    special: null,
    scope: '',
    level: '',
    shape: '',
    type: '',
    address: '',
    traffic: '',
    number: null,
    startTime: '',
    endTime: '',
    contact: '',
    phone: '',
    teacher: [
      {
        name: '',
        education: '',
        title: '',
        department: '',
        remarks: '',
      },
      {
        name: '',
        education: '',
        title: '',
        department: '',
        remarks: '',
      },
      {
        name: '',
        education: '',
        title: '',
        department: '',
        remarks: '',
      },
    ],
    cost: '',
    plan: '',
    budget: '',
    desc: '',
    fileLists: [] as fileLists[],
    categoryId: null,
    instructorId: null,
  })
  const rules = reactive<FormRules>({
    name: [{ required: true, message: '请输入竞赛名称', trigger: 'blur' }],
    conductUnit: [{ required: true, message: '请输入承办单位', trigger: 'blur' }],
    organizer: [{ required: true, message: '请输入主办单位', trigger: 'blur' }],
    special: [{ required: true, message: '请输入专业', trigger: 'blur' }],
    scope: [{ required: true, message: '请输入比赛范围', trigger: 'blur' }],
    level: [{ required: true, message: '请输入竞赛级别', trigger: 'blur' }],
    shape: [{ required: true, message: '请输入竞赛形式', trigger: 'blur' }],
    type: [{ required: true, message: '请输入竞赛类别', trigger: 'blur' }],
    address: [{ required: true, message: '请输入竞赛地点', trigger: 'blur' }],
    traffic: [{ required: true, message: '请输入交通方式', trigger: 'blur' }],
    number: [
      { required: true, message: '请输入参赛人数', trigger: 'blur' },
      { pattern: /^\d+$/, message: '参赛人数必须为数字', trigger: 'blur' },
    ],
    contact: [{ required: true, message: '请输入负责人', trigger: 'blur' }],
    phone: [
      { required: true, message: '请输入联系电话', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
    ],
    // 人员信息

    // 具体信息
    cost: [{ required: true, message: '请输入经费来源', trigger: 'blur' }],
    plan: [{ required: true, message: '请输入经费使用计划', trigger: 'blur' }],
    budget: [{ required: true, message: '请输入预算', trigger: 'blur' }],
    startTime: [{ required: true, message: '请选择竞赛开始时间', trigger: 'blur' }],
    endTime: [{ required: true, message: '请选择竞赛结束时间', trigger: 'blur' }],
    // 最少上传一个文件
    fileLists: [{ required: true, message: '请上传相关附件', trigger: 'blur' }],
  })

  // 校验表单
  const validateForm = (): Promise<boolean> => {
    return new Promise((resolve, reject) => {
      if (!refForm.value) {
        reject(new Error('表单实例不存在'))
        return
      }
      nextTick(() => {
        refForm.value!.validate((valid) => {
          if (valid) {
            resolve(true)
          } else {
            reject(new Error('表单校验失败'))
          }
        })
      })
    })
  }

  // 校验规则是否通过
  const checkRules = (): Promise<boolean> => {
    return new Promise((resolve, reject) => {
      validateForm()
        .then(() => {
          resolve(true)
        })
        .catch((error) => {
          import('element-plus').then(({ ElMessage }) => {
            ElMessage.error('请填写完整信息')
          })
          reject(error)
        })
    })
  }

  return {
    refForm,
    formBase,
    rules,
    checkRules,
    validateForm,
  }
}
